package ar.com.guess.image;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import ar.com.guess.image.enums.Constant;
import ar.com.guess.image.enums.LanguagePlay;
import ar.com.guess.image.enums.TypeUse;

/**
 * 
 * @author dgattesco
 * 
 */
public class OptionsGameActivity extends Activity {

	private TypeUse typeToPlay = TypeUse.KEYWORD;
	private LanguagePlay lang = LanguagePlay.EN;

	@Override
	public void onCreate(Bundle instance) {
		super.onCreate(instance);
		setContentView(R.layout.options);
		this.loadSharePreferences();
	}

	public void onClickSave(View v) {
		RadioGroup languageGroup = (RadioGroup) super
				.findViewById(R.id.languageGroup);
		int checkedRadioButtonLanguage = languageGroup
				.getCheckedRadioButtonId();
		switch (checkedRadioButtonLanguage) {
		case R.id.languageEnglish:
			lang = LanguagePlay.EN;
			break;
		case R.id.languageSpanish:
			lang = LanguagePlay.ES;
			break;
		}
		RadioGroup typeUseGroup = (RadioGroup) super
				.findViewById(R.id.typeUseGroup);
		int checkedRadioButtonType = typeUseGroup.getCheckedRadioButtonId();
		switch (checkedRadioButtonType) {
		case R.id.typeUseGesture:
			typeToPlay = TypeUse.GESTURE;
			break;
		case R.id.typeUseKeyword:
			typeToPlay = TypeUse.KEYWORD;
			break;
		}
		this.saveAndBackMenu();
	}

	public void saveAndBackMenu() {

		SharedPreferences settings = getSharedPreferences(Constant.DATA_SHARE.name(), MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constant.PARAM_LANGUAGE.getValue(), this.lang.name());
		editor.putString(Constant.PARAM_USE_TYPE.getValue(), this.typeToPlay.name());
		editor.commit();
		super.finish();
	}
	
	public void onClickClose(View v){
		super.finish();
	}
	
	private void loadSharePreferences(){
		
		SharedPreferences settings = getSharedPreferences(Constant.DATA_SHARE.name(), MODE_PRIVATE);
		String typeParam = settings.getString(Constant.PARAM_USE_TYPE.getValue(),TypeUse.KEYWORD.name());
		if (typeParam != null){
			RadioGroup typeGroup = (RadioGroup) super.findViewById(R.id.typeUseGroup);
			if (TypeUse.GESTURE.name().equals(typeParam)){
				typeGroup.check(R.id.typeUseGesture);
			}else{
				typeGroup.check(R.id.typeUseKeyword);
			}
		}
    	String langParam = settings.getString(Constant.PARAM_LANGUAGE.getValue(),LanguagePlay.EN.getLang());
    	if (langParam != null){
			RadioGroup languageGroup = (RadioGroup) super.findViewById(R.id.languageGroup);
			if (LanguagePlay.ES.name().equals(langParam)){
				languageGroup.check(R.id.languageSpanish);
			}else{
				languageGroup.check(R.id.languageEnglish);
			}
		}
	}
}
